<?php
/**
 * <i>@switch</i>组件
 * 
 * 可以使用的参数有：
 * - x-var mixed 变量
 * 
 * 示例：
 *  模板：
 * <code>
 * <span com="@switch" x-var="$integer" >
 * 	<span com="@case" x-value="1">1</span>
 * 	<span com="@case" x-value="2">2</span>
 * 	<span com="@case" x-value="3">3</span>
 *  <span com="@case">4</span>
 * </span>
 * </code>
 *
 * @version $Id: IMintSwitchComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 * @since 0.0.3
 */
class IMintSwitchComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		return array("x-var");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		if (!isset($parameters["x-var"])) {
			$this->throwUndefinedValueException("x-var");
			return false;
		}
		$xVar = IMintVariable::getDefinition($parameters["x-var"]);
		if ($xVar == null) {
			$this->throwVariableException("x-var");
		}
		
		$beginText = "<?php \n switch({$xVar}) {\n ?>";
		
		//将它和第一个@case之间的文本去除
		$childNodes = $this->getNode()->childNodes;
		if ($childNodes != null) {
			foreach ($childNodes as $childNode) {
				$nodeType = $childNode->nodeType;
				if ($nodeType != XML_ELEMENT_NODE || $nodeType->getAttribute(IMintConfig::$componentAttributeName) == null || $nodeType->getAttribute(IMintConfig::$componentAttributeName) != "@case") {
					$this->getNode()->removeChild($childNode);
				}
			}
		}
		
		$endText = "<?php } ?>";
		$this->addAroundScript($beginText, $endText);
		return true;
	}
}

?>